import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'

export interface Permission {
  id: number
  name: string
  code: string
  resource: string
  action: string
  description?: string
  created_at: string
  updated_at: string
}

export interface CreatePermissionForm {
  name: string
  code: string
  resource: string
  action: string
  description?: string
}

export interface UpdatePermissionForm {
  id: number
  name?: string
  code?: string
  resource?: string
  action?: string
  description?: string
}

export const usePermissionStore = defineStore('permission', () => {
  const permissions = ref<Permission[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)
  const total = ref(0)
  const resources = ref<string[]>([])
  const actions = ref<string[]>([])

  async function fetchPermissions(params: {
    page?: number
    pageSize?: number
    search?: string
    resource?: string
  } = {}) {
    loading.value = true
    error.value = null
    
    try {
      const response = await axios.get('/api/permissions/', { params })
      permissions.value = response.data.permissions
      total.value = response.data.total
      return { success: true }
    } catch (err: any) {
      error.value = err.response?.data?.message || '获取权限列表失败'
      return { success: false, error: error.value }
    } finally {
      loading.value = false
    }
  }

  async function createPermission(permissionData: CreatePermissionForm) {
    loading.value = true
    error.value = null
    
    try {
      const response = await axios.post('/api/permissions/', permissionData)
      permissions.value.push(response.data)
      return { success: true, permission: response.data }
    } catch (err: any) {
      error.value = err.response?.data?.message || '创建权限失败'
      return { success: false, error: error.value }
    } finally {
      loading.value = false
    }
  }

  async function updatePermission(permissionData: UpdatePermissionForm) {
    loading.value = true
    error.value = null
    
    try {
      const response = await axios.put(`/api/permissions/${permissionData.id}`, permissionData)
      const index = permissions.value.findIndex(p => p.id === permissionData.id)
      if (index !== -1) {
        permissions.value[index] = response.data
      }
      return { success: true, permission: response.data }
    } catch (err: any) {
      error.value = err.response?.data?.message || '更新权限失败'
      return { success: false, error: error.value }
    } finally {
      loading.value = false
    }
  }

  async function deletePermission(permissionId: number) {
    loading.value = true
    error.value = null
    
    try {
      await axios.delete(`/api/permissions/${permissionId}`)
      permissions.value = permissions.value.filter(p => p.id !== permissionId)
      return { success: true }
    } catch (err: any) {
      error.value = err.response?.data?.message || '删除权限失败'
      return { success: false, error: error.value }
    } finally {
      loading.value = false
    }
  }

  async function getPermissionRoles(permissionId: number) {
    loading.value = true
    error.value = null
    
    try {
      const response = await axios.get(`/api/permissions/${permissionId}/roles`)
      return { success: true, roles: response.data.roles }
    } catch (err: any) {
      error.value = err.response?.data?.message || '获取权限角色失败'
      return { success: false, error: error.value }
    } finally {
      loading.value = false
    }
  }

  async function fetchResources() {
    loading.value = true
    error.value = null
    
    try {
      const response = await axios.get('/api/permissions/resources/list')
      resources.value = response.data.resources
      return { success: true }
    } catch (err: any) {
      error.value = err.response?.data?.message || '获取资源列表失败'
      return { success: false, error: error.value }
    } finally {
      loading.value = false
    }
  }

  async function fetchActions() {
    loading.value = true
    error.value = null
    
    try {
      const response = await axios.get('/api/permissions/actions/list')
      actions.value = response.data.actions
      return { success: true }
    } catch (err: any) {
      error.value = err.response?.data?.message || '获取操作列表失败'
      return { success: false, error: error.value }
    } finally {
      loading.value = false
    }
  }

  return {
    permissions,
    loading,
    error,
    total,
    resources,
    actions,
    fetchPermissions,
    createPermission,
    updatePermission,
    deletePermission,
    getPermissionRoles,
    fetchResources,
    fetchActions
  }
})
